﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RH.Web.ViewModels;
using RH.Common.Extensions;

namespace RH.Web.Utility
{
    public static class ZGLHelper
    {
        /// <summary>
        /// Checks whethear a ZGL DataType is BIT or a Boolean
        /// </summary>
        /// <param name="dataType"></param>
        /// <returns></returns>
        public static bool IsBit(String dataType)
        {
            return (String.Compare(dataType, "BIT", true) == 0);
        }
        /// <summary>
        /// Checks whethear a ZGL DataType is Integer or any Other Numeric
        /// </summary>
        /// <param name="dataType"></param>
        /// <returns></returns>
        public static bool IsNumeric(String dataType)
        {
            return (String.Compare(dataType, "INT", true) == 0 || String.Compare(dataType, "NUM", true) == 0);
        }

        /// <summary>
        /// Checks whethear a ZGL DataType is VARCHAR
        /// </summary>
        /// <param name="dataType"></param>
        /// <returns></returns>
        public static bool IsVarchar(String dataType)
        {
            return (String.Compare(dataType, "VARCHAR", true) == 0);
        }


        /// <summary>
        /// Generates Unobtrusive JS Validation attributes for dynamically generated input and selelct controls
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        public static MvcHtmlString GenerateValidationAttributes(ZGLModelGroupProperty property)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            ///If the field is a required field
            if (property.IsRequired.ToBoolean())
            {
                String validationMessage = property.ValidationMessage.IsNullOrEmpty() ? String.Format("Please enter {0}.", property.Label) : property.ValidationMessage;
                if (property.Option.Count > 0 || !property.ListProvider.IsNullOrEmpty())
                    validationMessage = property.ValidationMessage.IsNullOrEmpty() ? String.Format("Please select {0}.", property.Label) : property.ValidationMessage;

                sb.AppendFormat("data-val-required=\"{0}\" ", validationMessage);
            }
            ///If the field is a Numeric field
            if (IsNumeric(property.DataType))
            {
                String validationMessage = property.ValidationMessage.IsNullOrEmpty() ? String.Format("Please enter numeric value for {0}.", property.Label) : property.ValidationMessage;
                if (sb.Length > 0)
                    sb.Append(" ");
                sb.AppendFormat("data-val-number=\"{0}\" ", validationMessage);
                sb.AppendFormat("data-val-range-max=\"2147483647\" ", validationMessage);
                sb.AppendFormat("maxlength=\"12\" ");
            }
            if (sb.Length > 0)
            {
                sb.AppendFormat("data-val=\"true\" ");
            }
            if (sb.Length > 0)
                return MvcHtmlString.Create(String.Format(" {0}", sb.ToString()));

            return MvcHtmlString.Empty;

        }
        /// <summary>
        /// Creates the necessary attribute to show a selected value for dropdownlist. 
        /// </summary>
        /// <param name="propertyValue"></param>
        /// <param name="currentValue"></param>
        /// <returns></returns>
        public static MvcHtmlString GetSelected(String propertyValue, String currentValue)
        {
            if (String.Compare(propertyValue, currentValue, true) == 0)
                return MvcHtmlString.Create(" selected=\"selected\" ");
            return MvcHtmlString.Empty;
        }
    }
}